//
//  AFHttpTool.m
//  RCloud_liv_demo
//
//  Created by Liv on 14-10-22.
//  Copyright (c) 2014年 RongCloud. All rights reserved.
//


#import "AFHttpTool.h"
//#define ContentType @"text/plain"
#define ContentType @"text/html"

@implementation AFHttpTool

+ (void)requestWihtMethod:(RequestMethodType)methodType
                      url:(NSString*)url
                   params:(NSDictionary*)params
                  success:(void (^)(id response))success
                  failure:(void (^)(NSError* err))failure
{
    NSURL* baseURL = [NSURL URLWithString:HttpUrlString];
    //获得请求管理者
    
    AFHTTPSessionManager* mgr = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json",@"text/javascript", @"text/plain", nil];
    
    switch (methodType) {
        case RequestMethodTypeGet:
        {
            //GET请求
            [mgr GET:url
          parameters:params
            progress:^(NSProgress * _Nonnull downloadProgress) {
                
            }
             success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                 if (success) {
                     
                     success(responseObject);
                 }
             }
             failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                 if (failure) {
                     failure(error);
                 }
             }];
            
            
        }
            break;
        case RequestMethodTypePost:
        {
            //POST请求
            [mgr POST:url
           parameters:params
             progress:^(NSProgress * _Nonnull uploadProgress) {
                 
             }
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  if (success) {
                      success(responseObject);
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  if (failure) {
                      failure(error);
                  }
              }];
        }
            break;
        default:
            break;
    }
}

+ (void)requestFriend:(NSString *)userId
                token:(NSString *)token
             friendId:(NSString*)friendId
            addRemark:(NSString*)remark
              success:(void (^)(id))success
              failure:(void (^)(NSError *))failure {
    NSDictionary *params = @{@"uid":userId,@"token":token,@"friendId":friendId,@"addRemark":remark};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"friend/addFriend"
                           params:params
                          success:success
                          failure:failure];
    
}
+ (void)searchFriendWithUserId:(NSString *)userId
                         token:(NSString *)token
                      nickName:(NSString *)nickName
                       pageNum:(NSInteger)pageNum
                      pageSize:(NSInteger)pageSize
                       success:(void (^)(id))success
                       failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":userId,@"token":token,@"nickName":nickName,@"pageNum":@(pageNum),@"pageSize":@(pageSize)};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"friend/searchUsersList"
                           params:params
                          success:success
                          failure:failure];
}
#pragma mark 获取好友列表
+ (void)getFriendListFromServerWithUid:(NSString *)uid
                                 token:(NSString *)token
                              keyWords:(NSString *)keyWords
                              pageSize:(NSInteger)pageSize
                               pageNum:(NSInteger)pageNum
                               success:(void (^)(id))success
                               failure:(void (^)(NSError *))failure {
    NSDictionary *params = [NSDictionary new];
    if (pageNum == 0 && pageSize == 0) {
        params = @{@"uid":uid,@"token":token,@"keyWords":keyWords};
    } else {
        params = @{@"uid":uid,@"token":token,@"keyWords":keyWords,@"pageSize":@(pageSize),@"pageNum":@(pageNum)};
    }
    
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"friend/myFriendsList"
                           params:params
                          success:success
                          failure:failure];
    
}
#pragma mark 新朋友列表
+ (void)getNewFriendListWithUid:(NSString *)uid
                          token:(NSString *)token
                        success:(void (^)(id))success
                        failure:(void (^)(NSError *))failure {
    NSDictionary *params = @{@"uid":uid,@"token":token};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"friend/newFriendsList"
                           params:params
                          success:success
                          failure:failure];
    
    
}
#pragma mark 处理请求加好友
+ (void)processRequestFriendWithFriendUid:(NSString *)friendUid
                                      uid:(NSString *)uid
                                    token:(NSString *)token
                                addStatus:(NSString *)addStatus
                                  success:(void (^)(id))success
                                  failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"friendId":friendUid,@"uid":uid,@"token":token,@"addStatus":addStatus};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"friend/dealNewFriend"
                           params:params
                          success:success
                          failure:failure];
}
#pragma mark 获取好友信息
+ (void)getUserInfoByUid:(NSString *)uid
                   token:(NSString *)token
               searchUid:(NSString *)searchUid
                 success:(void (^)(id))success
                 failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":searchUid};
    
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"userInfo"
                           params:params
                          success:success failure:failure];
}
#pragma mark 创建群组
+ (void)createGroupWithUid:(NSString *)uid
                     token:(NSString *)token
              groupUsersId:(NSString *)groupUsersId
                  groupImg:(NSString *)groupImg
                 groupName:(NSString *)groupName
                   success:(void (^)(id))success
                   failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token,@"groupUsersId":groupUsersId,@"groupImg":groupImg,@"groupName":groupName};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"group/createGroup"
                           params:params
                          success:success
                          failure:failure];
}

#pragma mark 更新群组信息
+ (void)updateGroupByUid:(NSString *)uid
                   token:(NSString *)token
                 groupId:(NSString *)groupId
            WithGroupImg:(NSString *)groupImg
               groupName:(NSString *)groupName
                 success:(void (^)(id))success
                 failure:(void (^)(NSError *))failure {
    NSDictionary *params = @{@"uid":uid,@"token":token,@"groupId":groupId,@"groupImg":groupImg,@"groupName":groupName};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"group/updateGroupInfo"
                           params:params
                          success:success
                          failure:failure];
}
#pragma mark 查询我的群组
+ (void)getMyGroupsByUid:(NSString *)uid
                   token:(NSString *)token
                 success:(void (^)(id))success
                 failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"group/myGroupsList"
                           params:params
                          success:success
                          failure:failure];
    
}
#pragma mark 获取群信息
+ (void)getGroupInfoByUid:(NSString *)uid
                    token:(NSString *)token
                  groupId:(NSString *)groupId
                  success:(void (^)(id))success
                  failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token,@"groupId":groupId};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"group/getGroupInfo"
                           params:params
                          success:success
                          failure:failure];
}
#pragma mark 清除好友申请列表
+ (void)cleanNewFriendsApplyListByUid:(NSString *)uid
                                token:(NSString *)token
                           userIdSets:(NSString *)userIdSets
                              success:(void (^)(id))success
                              failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token,@"userIdSets":userIdSets};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"friend/cleanNewFriends"
                           params:params
                          success:success
                          failure:failure];
}
#pragma mark 获取群成员列表
+ (void)getGroupMemberListByUid:(NSString *)uid
                          token:(NSString *)token
                        groupId:(NSString *)groupId
                        success:(void (^)(id))success
                        failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token,@"groupId":groupId};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"group/getGroupMemList"
                           params:params
                          success:success
                          failure:failure];
    
}
#pragma mark 群成员变动
+ (void)updateGroupMemberByUid:(NSString *)uid
                         token:(NSString *)token
                       groupId:(NSString *)groupId
                  withOperType:(NSString *)operType
                   groupUserId:(NSString *)groupUserId
                       success:(void (^)(id))success
                       failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token,@"groupId":groupId,@"operType":operType,@"groupUserId":groupUserId};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"group/updateGroupMem"
                           params:params
                          success:success
                          failure:failure];
}
#pragma mark 4.7.12.	管理权转让接口 T061
+ (void)attornGroupMgrByUid:(NSString *)uid
                      token:(NSString *)token
                    groupId:(NSString *)groupId
              withCreateUid:(NSString *)createUid
                    success:(void (^)(id))success
                    failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token,@"groupId":groupId,@"createUid":createUid};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"group/attornGroupMgr"
                           params:params
                          success:success
                          failure:failure];
    
}
#pragma mark 删除好友接口
+ (void)deleteFriendByUid:(NSString *)uid
                    token:(NSString *)token
                 friendId:(NSString *)friendId
                  success:(void (^)(id))success
                  failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token,@"friendId":friendId};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"friend/delFriend"
                           params:params
                          success:success
                          failure:failure];
    
}
#pragma mark 退出群
+ (void)quitGroupByUid:(NSString *)uid
                 token:(NSString *)token
               groupId:(NSString *)groupId
               success:(void (^)(id))success
               failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token,@"groupId":groupId};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"group/quitGroup"
                           params:params
                          success:success
                          failure:failure];
    
}
#pragma mark 注册用户查询
+ (void)searchRegUsersListByUid:(NSString *)uid
                          token:(NSString *)token
                    phoneNumber:(NSString *)phoneNumber
                        success:(void (^)(id))success
                        failure:(void (^)(NSError *))failure
{
    if (KCNSSTRING_ISEMPTY(phoneNumber)) {
        if (failure) {
            NSError *error = nil;
            failure(error);
            return;
        }
    }
    NSDictionary *params = @{@"uid":uid,@"token":token,@"phoneNumber":phoneNumber};
    
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"friend/searchRegUsersList"
                           params:params
                          success:success
                          failure:failure];
}
#pragma mark 图片上传
+ (void)uploadImageWith:(UIImage *)img
              imgBelong:(NSString *)imgBelong
                success:(void (^)(id))success
                failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"imgBelong":imgBelong};
    NSData *imageData = UIImageJPEGRepresentation(img, 1);
    float length = [imageData length]/1000;
    float scale = 0.0f;
    if (length > 450) {
        scale = 450.0f /length;
    }else{
        scale = 1.0f;
    }
    
    NSData *fData = UIImageJPEGRepresentation(img, scale);
    NSURL* baseURL = [NSURL URLWithString:HttpUrlString];
    //获得请求管理者
    
    AFHTTPSessionManager* mgr = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json",@"text/javascript", @"text/plain", nil];
    [mgr     POST:@"common/uploadImg"
       parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData)
     {
         [formData appendPartWithFileData:fData name:@"file" fileName:@"image" mimeType:@"image/jpeg"];
     }
         progress:^(NSProgress * _Nonnull uploadProgress)
     {
         
     }
          success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         if (success) {
             success(responseObject);
         }
     }
          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         if (failure) {
             failure(error);
         }
     }];
}
#pragma mark 获取通知的消息
+ (void)getFirstNotificationByUid:(NSString *)uid
                            token:(NSString *)token
                          success:(void (^)(id))success
                          failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost
                              url:@"notify/getFirstNotification"
                           params:params success:success
                          failure:failure];
}
#pragma mark 获取评论消息列表
+ (void)getCommentMsgsListByUid:(NSString *)uid
                          token:(NSString *)token
                          msgId:(NSString *)msgId
                        success:(void (^)(id))success
                        failure:(void (^)(NSError *))failure
{
    if (msgId.length == 0) {
        msgId = @"";
    }
    
    NSDictionary *params = @{@"uid":uid,@"token":token,@"msgId":msgId};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"notify/getCommentMsgsList" params:params success:success failure:failure];
    
}
#pragma mark 回复接口
+ (void)addCommentByUid:(NSString *)uid
                  token:(NSString *)token
              commentId:(NSInteger)commentId
               targetId:(NSString *)targetId
            withComment:(NSString *)comment
                success:(void (^)(id))success
                failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token,@"commentId":@(commentId),@"targetId":targetId,@"comment":comment};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"common/addFcomment" params:params success:success failure:failure];
    
}
#pragma mark 查看回复列表
+ (void)getFcommentMsgsListByUid:(NSString *)uid
                           token:(NSString *)token
                           msgId:(NSString *)msgId
                         success:(void (^)(id))success
                         failure:(void (^)(NSError *))failure
{
    if (msgId.length == 0) {
        msgId = @"";
    }
    NSDictionary *params = @{@"uid":uid,@"token":token,@"msgId":msgId};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"notify/getFcommentMsgsList" params:params success:success failure:failure];
}
#pragma mark 查看点赞列表
+ (void)getLikeMsgListByUid:(NSString *)uid token:(NSString *)token msgId:(NSString *)msgId success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    
    if (msgId.length == 0) {
        msgId = @"";
    }
    NSDictionary *params = @{@"uid":uid,@"token":token,@"msgId":msgId};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"notify/getLikeMsgsList" params:params success:success failure:failure];
}
#pragma mark 查看粉丝列表
+ (void)queryFansListByUid:(NSString *)uid
                     token:(NSString *)token
                  seachUid:(NSString *)seachUid
                  pageSize:(NSString *)pageSize
                   pageNum:(NSString *)pageNum
                   success:(void (^)(id))success
                   failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token,@"seachUid":seachUid,@"pageSize":pageSize,@"pageNum":pageNum};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"user/queryFansList" params:params success:success failure:failure];
    
}
#pragma mark 添加关注
+ (void)addAttenByUid:(NSString *)uid
                token:(NSString *)token
         attentionUid:(NSString *)attentionUid
              success:(void (^)(id))success
              failure:(void (^)(NSError *))failure
{
    NSDictionary *params = @{@"uid":uid,@"token":token,@"attentionUid":attentionUid};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"user/addAtten" params:params success:success failure:failure];
}
#pragma mark 查询关注列表
+ (void)queryAttenListByUid:(NSString *)uid token:(NSString *)token seachUid:(NSString *)seachUid pageSize:(NSString *)pageSize pageNum:(NSString *)pageNum success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    NSDictionary *params = @{@"uid":uid,@"token":token,@"seachUid":seachUid,@"pageSize":pageSize,@"pageNum":pageNum};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"user/queryAttenList" params:params success:success failure:failure];
}
#pragma mark 取消关注
+ (void)delAttenByUid:(NSString *)uid token:(NSString *)token attentionUid:(NSString *)attentionUid success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    NSDictionary *params = @{@"uid":uid,@"token":token,@"attentionUid":attentionUid};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"user/delAtten" params:params success:success failure:failure];
}
#pragma mark 查询收藏列表
+ (void)queryMyCollectionByUid:(NSString *)uid token:(NSString *)token pageSize:(NSString *)pageSize pageNum:(NSString *)pageNum success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    NSDictionary *params = @{@"uid":uid,@"token":token,@"pageSize":pageSize,@"pageNum":pageNum};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"user/queryMyCollection" params:params success:success failure:failure];
}
#pragma mark 取消收藏
+ (void)delCollectionByUid:(NSString *)uid token:(NSString *)token type:(NSString *)type targetId:(NSString *)targetId success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    NSDictionary *params = @{@"uid":uid,@"token":token,@"targetId":targetId,@"type":type};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"common/delCollection" params:params success:success failure:failure];
}
#pragma mark 更新用户信息
+ (void)updateUserInfoByUid:(NSString *)uid token:(NSString *)token nickName:(NSString *)nickName myCheck:(NSString *)myCheck background:(NSString *)background success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    if (nickName.length == 0 || nickName == nil) {
        nickName = @"";
    }
    if (myCheck.length == 0 || myCheck == nil) {
        myCheck = @"";
    }
    if (background.length == 0 || background == nil) {
        background = @"";
    }
    
    NSDictionary *params = @{@"uid":uid,@"token":token,@"nickName":nickName,@"myCheck":myCheck, @"background" : background};
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"user/updateUserInfo" params:params success:success failure:failure];
}
#pragma mark 删除摄影
+ (void)delPhotoByUid:(NSString *)uid token:(NSString *)token photoId:(NSInteger)photoId success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    NSDictionary *params = @{@"uid":uid,@"token":token,@"photoId":@(photoId)};
    
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"photo/delPhoto" params:params success:success failure:failure];
}
#pragma mark 删除视频
+ (void)delVideoByUid:(NSString *)uid token:(NSString *)token videoId:(NSInteger)videoId success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    NSDictionary *params = @{@"uid":uid,@"token":token,@"videoId":@(videoId)};
    
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"video/delVideo" params:params success:success failure:failure];
}
#pragma mark 修改备注名
+ (void)modifyRemarkNameWithUid:(NSString *)uid token:(NSString *)token friendUid:(NSString *)friendUid remarkName:(NSString *)remarkName successBlock:(void (^)(BOOL, NSDictionary *))successBlock {
    NSDictionary *params = @{@"uid":uid,@"token":token,@"friendUid":friendUid,@"remarkName":remarkName};
   [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"friend/modifyRemarkName"
                          params:params
                         success:^(id response)
    {
        if (response) {
            NSString *returnCode = [NSString stringWithFormat:@"%@",response[@"returnCode"]];
            if ([returnCode isEqualToString:@"00"]) {
                NSLog(@"修改备注名%@",response);
                successBlock(YES,response);
            }else {
                NSLog(@"修改备注名错误返回%@",response[@"message"]);
                successBlock(NO,nil);
            }
        }

       
   }
                         failure:^(NSError *err) {
                             NSLog(@"修改备注名错误返回%@",err.description);
                             successBlock (NO,nil);
   }];
   
}
#pragma mark 上传位置
+ (void)uploadlocationWithUid:(NSString *)uid token:(NSString *)token longitude:(NSString *)longitude latitude:(NSString *)latitude currentLocation:(NSString *)currentLocation successBlock:(void (^)(BOOL, NSDictionary *))successBlock {
     NSDictionary *params = @{@"uid":uid,@"token":token,@"longitude":longitude,@"latitude":latitude,@"currentLocation":currentLocation};
   
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"user/location"
                           params:params
                          success:^(id response)
     {
         if (response) {
             NSString *returnCode = [NSString stringWithFormat:@"%@",response[@"returnCode"]];
             if ([returnCode isEqualToString:@"00"]) {
                 NSLog(@"上传位置%@",response);
                 successBlock(YES,response);
             }else {
                 NSLog(@"上传位置错误返回%@",response[@"message"]);
                 successBlock(NO,nil);
             }
         }
         
         
     }
                          failure:^(NSError *err) {
                              NSLog(@"上传位置错误返回%@",err.description);
                              successBlock (NO,nil);
                          }];
}
#pragma mark 查询朋友信息
+ (void)queryChatFriendInfoWithUid:(NSString *)uid token:(NSString *)token longitude:(NSString *)longitude latitude:(NSString *)latitude friendUid:(NSString *)friendUid successBlock:(void (^)(BOOL, NSDictionary *))successBlock {
    NSMutableDictionary *params =@{@"uid":uid,@"token":token,@"friendUid":friendUid}.mutableCopy;
    if (!KCNSSTRING_ISEMPTY(longitude)) {
        [params setObject:longitude forKey:@"longitude"];
        [params setObject:latitude forKey:@"latitude"];
    }
    
//    } else {
//         NSDictionary *params = @{@"uid":uid,@"token":token,@"longitude":longitude,@"latitude":latitude,@"friendUid":friendUid};
//    }
    
    
    [AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"user/queryChatFriendInfo"
                           params:params
                          success:^(id response)
     {
         if (response) {
             NSString *returnCode = [NSString stringWithFormat:@"%@",response[@"returnCode"]];
             if ([returnCode isEqualToString:@"00"]) {
                 NSLog(@"查询朋友信息%@",response);
                 successBlock(YES,response);
             }else {
                 NSLog(@"查询朋友信息错误返回%@",response[@"message"]);
                 successBlock(NO,nil);
             }
         }
         
         
     }
                          failure:^(NSError *err) {
                              NSLog(@"查询朋友信息错误返回%@",err.description);
                              successBlock (NO,nil);
                          }];
}
@end
